<?php
namespace api\modules\h5\controllers;

use common\enums\StatusEnum;
use common\helpers\ArrayHelper;
use common\models\banner\Banner;
use common\models\goods\GClass;
use common\models\goods\GoodsCommon;
use Yii;
use common\helpers\ResultDataHelper;
use api\controllers\OnAuthController;

/**
 * 首页接口
 *
 * Class IndexController
 * @package api\modules\h5\controllers
 */
class IndexController extends OnAuthController
{
    public $modelClass = '';

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $optional = ['index-all','recommend'];


    /**
     * 获取推荐商品
     * @param $store_id
     * @return array|\yii\db\ActiveRecord[]
     */
    private function getRecommend($store_id)
    {
        //推荐
        $recommend = GoodsCommon::find()
            ->where(['store_id'=>$store_id,'status'=>StatusEnum::ENABLED])
            ->andWhere(['goods_commend'=>1])
            ->select(['goods_commonid','goods_name','goods_code','goods_bar','goods_image','goods_unit','goods_price','goods_marketprice','goods_costprice','goods_discount'])
            ->asArray()
            ->limit(3)
            ->all();

        $count = count($recommend);

        if(!$recommend || $count < 3){
            $recommend_ext_array = GoodsCommon::find()
                ->where(['store_id'=>$store_id,'status'=>StatusEnum::ENABLED])
                ->andWhere(['goods_commend'=>0])
                ->select(['goods_commonid','goods_name','goods_code','goods_bar','goods_image','goods_unit','goods_price','goods_marketprice','goods_costprice','goods_discount'])
                ->orderBy(['goods_salenum'=>SORT_DESC])
                ->asArray()
                ->limit(10)
                ->all();

            if(count($recommend_ext_array) > (3 - $count)){
                shuffle($recommend_ext_array);
                $recommend_ext = array_slice($recommend_ext_array,0,(3 - $count));
            }else $recommend_ext = $recommend_ext_array;

            if($count == 0){
                $recommend = array_values($recommend_ext);
            }else{
                $recommend = array_values(array_merge($recommend,$recommend_ext));
            }
        }

        return $recommend;
    }

    /**
     * 推荐
     * @return array|mixed|\yii\db\ActiveRecord[]
     */
    public function actionRecommend(){
        $request = Yii::$app->request;
        $store_id = $request->get('store_id')??0;

        if(!$store_id)
            return ResultDataHelper::api(422, '商铺id不能为空');

        return $this->getRecommend($store_id);
    }

    /**
     * 获取首页数据
     * @return array|mixed
     */
    public function actionIndexAll()
    {
        $request = Yii::$app->request;
        $store_id = $request->get('store_id')??0;

        if(!$store_id)
            return ResultDataHelper::api(422, '商铺id不能为空');

        //Banner
        $banner = Banner::find()
            ->select(['id','title','image','type','link','relevance_id'])
            ->where(['store_id'=>$store_id,'status'=>StatusEnum::ENABLED])
            ->asArray()
            ->all();

        //推荐
        $recommend = $this->getRecommend($store_id);

        //分类
        $gclass = GClass::find()
            ->where(['store_id'=>$store_id,'status'=>StatusEnum::ENABLED])
            ->select(['id','store_id','name','parent_id','status'])
            ->asArray()
            ->all();

        $top_gclass = ArrayHelper::getTop($gclass,'parent_id');

        $first_gclass = $top_gclass[0]??null;
        if($first_gclass){
            $gclass_ids = ArrayHelper::getChildIds($gclass,$first_gclass['id'],'id','parent_id');
            $gclass_ids[] = $first_gclass['id'];

            $goods_list = GoodsCommon::find()
                ->select(['goods_commonid','goods_name','goods_code','goods_bar','goods_image','goods_unit','goods_price','goods_marketprice','goods_costprice','goods_discount'])
                ->where(['status'=>StatusEnum::ENABLED,'store_id'=>$store_id])
                ->andWhere(['in','gc_id',$gclass_ids])
                ->limit(10)
                ->asArray()
                ->all();

        }
        return [
            'banner'=>$banner,
            'recommend'=>$recommend,
            'gclass'=>$top_gclass,
            'goods_list'=>$goods_list??[]
        ];
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, ['view', 'update', 'create', 'delete']))
        {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }
}
